package com.base.cn.platform.os.controller.manage.group;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.web.LoginUtil;
import com.base.cn.platform.os.service.manage.group.SysGroupFunctionService;
import com.base.cn.platform.os.service.manage.group.SysGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 后台部门管理Controller
 *
 * @author s.li
 * @create 2018-04-03-11:03
 */
@Controller
@RequestMapping("/admin/manage/group")
public class AdminSysGroupController extends AdminBaseController {

    @Autowired
    private SysGroupService sysGroupService;
    @Autowired
    private SysGroupFunctionService sysGroupFunctionService;
    @Autowired
    private LoginUtil loginUtil;

    /**
     * 初始化选择部门的条件
     * @return 操作页面
     */
    @RequestMapping("/groupConditionList")
    public String groupConditionList(){
        return resultPage("/manage/group/sys_group_for_condition");
    }

    /**
     * 获取部门数据
     * @return Map<String,Object>
     */
    @RequestMapping("/ajaxGroupConditionList")
    @ResponseBody
    public Map<String,Object> ajaxGroupConditionList(){
        Map<String,Object> params =new HashMap<>();
        params.put("status",1);//查询状态正常的部门
        List<Map<String,Object>> sysGroupList = sysGroupService.findSysGroupList(params);
        return ResultUtil.SUCCESS(sysGroupList);
    }

    /**
     * 初始化部门列表页面
     * @return String
     */
    @RequestMapping("/groupList")
    public String groupList(){
        return resultPage("/manage/group/sys_group_list");
    }

    /**
     * 获取部门数据列表
     * @return Map<String,Object>
     */
    @RequestMapping("/ajaxGroupList")
    @ResponseBody
    public Map<String,Object> ajaxGroupList(){
        Map<String,Object> params =new HashMap<>();
        List<Map<String,Object>> sysGroupList = sysGroupService.findSysGroupList(params);
        return ResultUtil.SUCCESS(sysGroupList);
    }

    /**
     * 保存部门数据
     * @param request HttpServletRequest
     * @param sysGroup 部门数据对象
     * @return Map<String,Object>
     */
    @RequestMapping("/saveSysGroup")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "保存或修改部门数据")
    public Map<String,Object> saveSysGroup(HttpServletRequest request,
                                           @RequestBody Map<String, Object> sysGroup){
        //设置创建人ID
        sysGroup.put("createUserId",loginUtil.getLoginUserId(request));
        sysGroup.put("status",1);
        Map<String,Object> resultMap = sysGroupService.saveSysGroup(sysGroup);
        resultMap.put("parentId",sysGroup.get("parentId"));
        return resultMap;
    }

    /**
     * 初始保存部门页面
     * @param model
     * @param id 部门ID，修改时需要传入
     * @param parentId 父级ID，如果在父级上进行添加的需要传入
     * @return 操作页面
     */
    @RequestMapping(value = {"/initUpdate","/initAdd"})
    public String initUpdateOrAdd(Model model,
                                  @RequestParam(value = "id",required = false,defaultValue = "0") BigDecimal id,
                                  @RequestParam(value = "parentId",required = false,defaultValue = "0") BigDecimal parentId){
        Map<String,Object> sysGroup = null;
        if(DataUtil.idIsNotNull(id)){
            sysGroup = sysGroupService.findSysGroupById(id);
        }
        if(sysGroup==null){
            sysGroup = new HashMap<>();
            sysGroup.put("id",0);
            sysGroup.put("parentId",parentId);
        }
        model.addAttribute("sysGroup",sysGroup);

        Map<String,Object> params = new HashMap<>();
        params.put("groupId",id);
        List<Map<String,Object>> sysFunctionList = sysGroupFunctionService.findShowGroupFunctionList(params);
        model.addAttribute("sysFunctionList",sysFunctionList);

        return resultPage("/manage/group/save_sys_group");
    }

    /**
     * 修改部门数据状态
     * @param ids 部门ID串
     * @param status 状态，1正常，2禁用，3删除
     * @return Map<String,Object>
     */
    @RequestMapping("/updateSysGroupStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "修改部门数据状态")
    public Map<String,Object> updateSysGroupStatus(@RequestParam("ids") String ids,@RequestParam("status") int status){
        return  sysGroupService.updateSysGroupStatus(ids,status);
    }

    /**
     * 修改部门归属父级
     * @param id 部门ID
     * @param parentId 新父级ID
     * @return Map<String,Object>
     */
    @RequestMapping("/updateSysGroupParentId")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.SYS_USER,action = "修改部门归属父级")
    public Map<String,Object> updateSysGroupParentId(@RequestParam("id") BigDecimal id,@RequestParam("parentId") BigDecimal parentId){
        return sysGroupService.updateSysGroupParentId(id,parentId);
    }
}
